#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#define MAX_WORKER 5
typedef void (*work)();

void father_work1()
{
    printf("father doing work1\n");                                       
}

void father_work2()
{
    printf("father doing work2\n");
}

void father_work3()
{
    printf("father doing work3\n");
}
void worker()
{
    int cnt = 3;
    while(cnt--)
    {
        printf("child process, pid_t: %d, ppid_t: %d, cnt: %d\n", getpid(), getppid(), cnt);
        sleep(1);
    }
}
void initArray(work array[])
{
    for(size_t i = 0; i < MAX_WORKER; i++)
        array[i] = NULL;
}

void addWork(work array[], work w)
{
    for(size_t i = 0; i < MAX_WORKER; i++)
    {
        if(array[i] == NULL)
        {
            array[i] = w;
            break;
        }
    }
    return;
}

void doingWork(work array[])
{
    for(size_t i = 0; i < MAX_WORKER; i++)
        if(array[i]) array[i]();
}
int main()
{
    work array[MAX_WORKER]; 
    initArray(array);
    addWork(array, father_work1);
    addWork(array, father_work2);
    addWork(array, father_work3);
    pid_t id = fork();
    if(id == 0)
    {
        worker();
        exit(1);
    }
    else
    {
        while(1)
        {
            pid_t rid = waitpid(id, NULL, WNOHANG);
            if(rid > 0)
            {
                printf("wait success\n");
                break;
            }
            else if(rid == 0)
            {
                printf("father doing other thing\n");
                doingWork(array);
            }
            else
            {
                printf("wait failed\n");
                break;
            }
            sleep(1);
        }
    }
    return 0;
}
